package com.jay.lang.intf;

import java.nio.CharBuffer;
import java.util.Random;
import java.util.Scanner;

public class ReadableIntf {
	static class RandomWords implements Readable {
		private static Random rand = new Random(47);
		private static final char[] capitals = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
				.toCharArray();
		private static final char[] lowers = "abcdefghijklmnopqrstuvwxyz"
				.toCharArray();
		private static final char[] vowels = "aeiou".toCharArray();
		private int count;

		public RandomWords(int count) {
			this.count = count;
		}

		public int read(CharBuffer cb) { // 这里覆盖Readable的read方法
			// 把判断语句放在最前面，一执行，条件就成立的话CharBuffer中没有字符，调用hasNext就会返回假
			if (count-- == 0)
				return -1; // Indicates end of input
			cb.append(capitals[rand.nextInt(capitals.length)]);
			for (int i = 0; i < 4; i++) {
				cb.append(vowels[rand.nextInt(vowels.length)]);
				cb.append(lowers[rand.nextInt(lowers.length)]);
			}
			cb.append(" "); /* 用空格来做为定界符，把CharBuffer中的字符在空格处裁开，这样子就会得到数个next()值 */
			return 10; // Number of characters appended
		}
	}

	public static void main(String[] args) {
		Scanner s = new Scanner(new RandomWords(10));
		while (s.hasNext())
			System.out.println(s.next());
	}

}
